﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;

namespace mForum.Web.Common.Controllers
{
    using mForum.Core.Common;
    using mForum.Core.Messages.UserServiceMessages;
    using mForum.Core.DataTransferObject;
    using mForum.Core.Service;
    using mForum.Resources.Views;
    using mForum.Web.Common.Infrastructure;
    using mForum.Web.Common.ViewDataModel;

    [HandleError]
    public class AccountController : Controller
    {

        private IFormsAuthenticationService _formsService;
        private IUserService _userService;
        private IViewDataFactory _viewDataFactory;

        public AccountController(IViewDataFactory viewDataFactory, IUserService userService, IFormsAuthenticationService formsService)
        {
            Check.Argument.IsNotNull(userService, "userService");
            Check.Argument.IsNotNull(formsService, "formsService");
            Check.Argument.IsNotNull(viewDataFactory, "viewDataFactory");

            _userService = userService;
            _formsService = formsService;
            _viewDataFactory = viewDataFactory;
        }

        public ActionResult LogOn()
        {
            LogOnViewData view = _viewDataFactory.Create<LogOnViewData>();

            return View(view);
        }

        [HttpPost]
        public ActionResult LogOn(LogOnDTO dto, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                LogOnRequest request = new LogOnRequest()
                {
                    LogOnDTO = new LogOnDTO()
                    {
                        Username = dto.Username,
                        Password = dto.Password
                    }
                };

                LogOnResponse response = _userService.ValidateUser(request);

                if (response.isValidated)
                {
                    _formsService.SignIn(dto.Username, dto.RememberMe);

                    if (!String.IsNullOrEmpty(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", LogOn_aspx.IncorrectData);
                }
            }

            LogOnViewData view = _viewDataFactory.Create<LogOnViewData>();

            return View(view);
        }

        public ActionResult LogOff()
        {
            _formsService.SignOut();

            return RedirectToAction("Index", "Home");
        }

        public ActionResult Register()
        {
            RegisterViewData view = _viewDataFactory.Create<RegisterViewData>();
            view.MinPasswordLength = 5;

            return View(view);
        }

        [HttpPost]
        public ActionResult Register(RegisterDTO dto)
        {
            if (ModelState.IsValid)
            {

                RegisterResponse response = _userService.CreateUser(new RegisterRequest() { RegisterDTO = dto });

                if (response.UserDTO != null)
                {
                    _formsService.SignIn(dto.Username, false);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("", Register_aspx.AccountCreationFailed);
                }
            }

            RegisterViewData view = _viewDataFactory.Create<RegisterViewData>();
            view.UserName = dto.Username;
            view.PasswordQuestion = dto.PasswordQuestion;
            view.PasswordAnswer = dto.PasswordAnswer;
            view.Email = dto.Email;

            return View(view);
        }
    }
}
